<template>
	<div>
		<!-- <input type="file" @change="handleFileUpload" />

		<h1>Adjacent Value: {{ adjacentValue }}</h1>
		<div v-if="adjacentValue !== null">
			Adjacent Value: {{ adjacentValue }}
		</div> -->

		<input type="file" @change="handleFileUploads" />
		<span>
			{{ excelV }}
		</span>
	</div>
</template>

<script lang="ts" setup>
import { ref } from 'vue';
import * as XLSX from 'xlsx';
import api from '/@/api/sy/apiERP.js';
import {processFiles,ExcelPerms } from '/@/views/utils/excelUitl';

const excelData = ref(null);
const adjacentValue = ref(null);


// // excel 读取规范
// interface ExcelPerms {
//   excelName: string //excel要获取的名称
//   name : string //名称对应的变量名
// }
const ExcelList = ref<ExcelPerms[]>([
	{excelName:"客户名称",name:"custName"},
	{excelName:"客户联系方式",name:"custPhone"},
	{excelName:"数量",name:"number"},
	{excelName:"含税金额",name:"amount"},
	{excelName:"要求结束时间",name:"endTime"},
	{excelName:"跟踪状态",name:"trackingState"},
	{excelName:"备注",name:"remark"},
	{excelName:"付款方式",name:"paymentMethod"},
	{excelName:"项目类型",name:"projectType"},
	{excelName:"施工地址",name:"custAddress"},
	{excelName:"销售预测单",name:"salesForecastCode"},
	{excelName:"要求施工时间",name:"strTime"},
	{excelName:"实际结束日期",name:"actualEndTime"},
])


const getValList = ref([
	"客户名称",
	"客户联系方式",
	"数量",
	"含税金额",
	"要求结束时间",
	"跟踪状态",
	"备注",
	"付款方式",
	"项目类型",
	"施工地址",
	"销售预测单",
	"要求是施工时间",
	"实际结束日期",
])
const excelV = ref()
const handleFileUploads = (event)=>{
	// const file = event.target.files[0];
	// let a = processFile(file,"施工地址")
	// console.log(a);
	
	const file = event.target.files[0];
	processFiles(file,ExcelList.value).then((val)=>{
		console.log('val',val);
		
	})
	console.log(excelV.value);
}
const handleFileUpload = (event, getList) => {
	getList = getValList.value
	const file = event.target.files[0];
	if (!file) return; // 检查是否有文件被上传

	const reader = new FileReader();

	reader.onload = (e) => {
		const result = e.target?.result;
		if (result instanceof ArrayBuffer) {
			const data = new Uint8Array(result);
			const workbook = XLSX.read(data, { type: 'array' });
			const sheetName = workbook.SheetNames[0];
			const worksheet = workbook.Sheets[sheetName];

			// 将 Excel 数据转换为 JSON 对象
			const jsonData = XLSX.utils.sheet_to_json(worksheet, { header: 1 });

			// // 将 Excel 数据存储在组件数据中
			// excelData.value = jsonData;


			let returnList = new Map()
			getList.forEach(val => {
				// 查找目标字符串并获取旁边的值
				for (let rowIndex = 0; rowIndex < jsonData.length; rowIndex++) {
					const row = jsonData[rowIndex];
					const columnIndex = row.indexOf(val);
					if (columnIndex !== -1 && columnIndex + 1 < row.length) {
						adjacentValue.value = row[columnIndex + 1];

						returnList.set(val, adjacentValue.value)
						console.log(returnList);
						break;
					}
				}
			});

		};
	};

	reader.readAsArrayBuffer(file);
};
</script>